import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  // 用于变更 store中的数据
  mutations: {
    add1(state) {
      state.count++
    },
    addN(state, step) {
      state.count += step
    },
    // 减法操作s
    sub1(state) {
      state.count--
    },
    subN(state, step) {
      state.count -= step
    }
  },
  actions: {
    AsyncAdd1(context) {
      setTimeout(function() {
        context.commit('add1')
      }, 1000)
    },
    AsyncAddN(context, step) {
      setTimeout(() => {
        context.commit('addN', step)
      }, 1000)
    },
    AsyncSub1(context) {
      setTimeout(function() {
        context.commit('sub1')
      }, 1000)
    },
    AsyncSubN(context, step) {
      setTimeout(() => {
        context.commit('subN', step)
      }, 1000)
    }
  },
  modules: {},
  getters: {
    showNum(state) {
      return '当前最新的数量是-[' + state.count + ']'
    }
  }
})
